local jass = require 'jass.common'

local mt = ac.buff['生命周期']

mt.kill_when_remove = false
mt.keep = true
function mt:on_add()
	local target = self.owner

	if self.time <= 0 then
		self:remove()
		return
	end

	jass.UnitApplyTimedLife(target.handle, base.string2id('BTLF'), self.time + 0.1)
end

function mt:on_remove()
	local target = self.owner
	local is_remove_hide

	-- AHab 的光环无法作用于隐藏单位
	if target:has_restriction '隐藏' then
		target:remove_restriction '隐藏'
		is_remove_hide = true
	end

	target:add_ability 'AHab'
	target:remove_ability 'AHab'

	if is_remove_hide then
		target:add_restriction '隐藏'
	end

	if self.kill_when_remove then
		if target:is_alive() then
			target:kill(target)
		end
	end
end